import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.Set;

import java.io.PrintWriter;

public class DataProcessor {

    private static final String OUTPUT_FILE = "G:\\EpiDev\\JMPeripheral\\_test\\SunWei\\JMG4PatchsV2\\04_uploadYmShotUndoByListV2\\childNoBactIDList.txt";
    private static final String LOG_FILE = "C:\\temp\\log.txt";
    private static final String DATA_PATTERN = "^\\d{18}\\.[B]\\d{3}\\.(\\d{4}-\\d{2}-\\d{2})$";

    public static void main(String[] args) {
        Set<String> processedLines = new HashSet<>();
        try (PrintWriter outputWriter = new PrintWriter(OUTPUT_FILE);
             BufferedWriter logWriter = new BufferedWriter(new FileWriter(LOG_FILE))) {

            // 假设我们要处理的文件位于 C:\data 目录下
            Path directory = Paths.get("G:\\EpiDev\\JMPeripheral\\_test\\SunWei\\JMG4PatchsV2\\04_uploadYmShotUndoByListV2\\重复剂次名单");

            // 检查目录是否存在
            if (!Files.isDirectory(directory)) {
                System.out.println("Directory not found.");
                return;
            }

            Files.walk(directory)
                    .filter(path -> path.toString().endsWith(".txt")) // 只处理 .txt 文件
                    .forEach(filePath -> {
                        try {
                            processFile(filePath, outputWriter, logWriter, processedLines);
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                    });

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void processFile(Path filePath, PrintWriter outputWriter, BufferedWriter logWriter, Set<String> processedLines) throws IOException {
        try (BufferedReader reader = Files.newBufferedReader(filePath)) {
            String line;
            while ((line = reader.readLine()) != null) {
                if (line.trim().isEmpty()) { // 忽略空行
                    continue;
                }
                if (line.matches(DATA_PATTERN)) {
                    if (!processedLines.contains(line)) {
                        outputWriter.println(line);
                        processedLines.add(line);
                    }
                } else {
                    logWriter.write("Invalid data in file " + filePath + ": " + line + "\n");
                }
            }
        } catch (IOException e) {
            e.printStackTrace(); // 或者记录错误到日志文件
            logWriter.write("Error reading file " + filePath + ": " + e.getMessage() + "\n");
        }
    }
}
